package kz.sdu.edu.qms.ui;


import java.util.HashSet;

import kz.edu.sdu.itech.qms.Client;
import kz.edu.sdu.itech.qms.QMS;
import kz.edu.sdu.itech.qms.QMSProps;
import kz.edu.sdu.itech.qms.Queue;
import kz.edu.sdu.itech.qms.Serve;
import kz.edu.sdu.itech.qms.util.Logger;

import com.trolltech.qt.core.QRect;
import com.trolltech.qt.core.QTimer;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QCloseEvent;
import com.trolltech.qt.gui.QFont;
import com.trolltech.qt.gui.QPaintEvent;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QWidget;

public class MainWidget extends QWidget {
   static Logger log = new Logger(MainWidget.class);
   static QMS q = new QMS();

   static int W = 800;
   static int H = 600;
   static int SPACING = 5;
   static int CLIENT_HEIGHT = 30;
   static int CLIENT_WIDTH = 80;
   static QFont CLIENT_FONT = new QFont("Arial", 8);
   static QFont SERVE_FONT = new QFont("Arial", 10);

   QTimer timer;

   public static void main(String[] args) {
      QApplication.initialize(args);
      MainWidget testMainWidget = new MainWidget(null);
      testMainWidget.show();
      QApplication.exec();
   }

   public MainWidget(QWidget parent){
      super(parent);
      timer = new QTimer();
      timer.setInterval(1000);

      timer.timeout.connect(this, "restartTimer()");
      setFixedSize(W, H);
      setVisible(true);
      timer.start();
   }

   public void restartTimer() {
      timer.startTimer(1000);
      log.debug("TIMER RESTART");
      update();
   }

   void drawClient(QPainter painter, int x1, int y1, int w, int h, Client c) {
      painter.drawRect(x1, y1, w, h);
      painter.setFont(CLIENT_FONT);
      if (c != null) {
         painter.drawText(x1, y1, w, h / 2, Qt.AlignmentFlag.AlignCenter.value(), c.getClientName());
         String ticket = c.getClientTicket();
         if (ticket != null && ticket.length() > 0) {
            painter.drawText(x1, y1 + h / 2, w, h / 2, Qt.AlignmentFlag.AlignCenter.value(), c.getClientTicket());
         } else {
            painter.drawText(x1, y1 + h / 2, w, h / 2, Qt.AlignmentFlag.AlignCenter.value(), "wait");
         }
      } else {
         painter.drawText(x1, y1, w, h, Qt.AlignmentFlag.AlignCenter.value(), "empty");
      }
   }

   void drawQueue(QPainter painter, int x1, int y1, int w, int h, Queue quee) {
      int n = quee.size();
      if (n == 0)
         return;
      int spacing = SPACING;
      int ph = CLIENT_HEIGHT;
      int rh = ph - spacing;
      int rw = w - 2 * spacing;
      for (int i = 0; i < n; i++) {
         drawClient(painter, x1 + spacing, i * ph + y1 + spacing, rw, rh, quee.get(i));
      }

   }

   void drawNoTicketGuys(QPainter painter, int x1, int y1, int w, int h, int n, HashSet<Client> set) {
      int m = QMSProps.QUEUE_SIZE;
      int x = 0, y = 0;
      for (Client c : set) {
         drawClient(painter, x1 + x * CLIENT_WIDTH, y1 + y * CLIENT_HEIGHT, CLIENT_WIDTH, CLIENT_HEIGHT, c);
         y = y + 1;
         if (y >= m) {
            y = 0;
            x = x + 1;
         }
      }
   }

   void drawWaitingArea(QPainter painter, int x1, int y1, int w, int h, int extra) {
      painter.drawRect(x1, y1, w, h);
      int cw = CLIENT_WIDTH;
      int rw = cw - SPACING;
      for (int i = 0; i < 5; i++) {
         drawQueue(painter, x1 + cw * i, y1, rw, h, q.getQ(i));
      }
      int s = x1+cw*QMSProps.QUEUE_NUM;
      drawNoTicketGuys(painter, s, y1, w - s, h, extra, QMS.set);
   }

   void drawServeLine(QPainter painter, int x1, int y1, int w, int h) {
      int flag = Qt.AlignmentFlag.AlignCenter.value();
      for (int i = 0; i < QMSProps.QUEUE_NUM; i++) {
         Serve s = q.getS(i);
         painter.setFont(SERVE_FONT);
         painter.drawText(x1 + CLIENT_WIDTH * i, y1, CLIENT_WIDTH, CLIENT_HEIGHT, flag, String.format("S%d", s.getDID()));
         drawClient(painter, x1 + CLIENT_WIDTH * i, y1 + CLIENT_HEIGHT, CLIENT_WIDTH, CLIENT_HEIGHT, s.getCurClient());
      }
   }

   void drawStat(QPainter painter, int x1, int y1, int w, int h) {
      painter.drawRect(x1 - 5, y1 - 5, w + 5, h + 5);
      int flag = Qt.AlignmentFlag.AlignLeft.value();
      int he = h / 4;
      painter.setFont(new QFont("Arial", 16));
      painter.drawText(x1, y1, w, he, flag, String.format("TOTAL: %d", QMSProps.clients_total));
      painter.drawText(x1, y1 + he, w, he, flag, String.format("IN QUEUE: %d", QMSProps.clients_in_queues));
      painter.drawText(x1, y1 + 2 * he, w, he, flag, String.format("SERVED: %d", QMSProps.clients_served));
      painter.drawText(x1, y1 + 3 * he, w, he, flag, String.format("NO TICKET: %d", QMSProps.clients_waiting));
   }

   @Override
   protected void paintEvent(QPaintEvent e){
      QPainter painter = new QPainter(this);
      drawServeLine(painter, 10, 10, QMSProps.QUEUE_NUM * CLIENT_WIDTH, 2 * CLIENT_HEIGHT);
      drawWaitingArea(painter, 10, 210, (QMSProps.QUEUE_NUM + 4) * CLIENT_WIDTH, (QMSProps.QUEUE_SIZE) * CLIENT_HEIGHT, 4);
      drawStat(painter, QMSProps.QUEUE_NUM * CLIENT_WIDTH + 100, 10, 300, 100);
      painter.end();
   }

   @Override
   protected void closeEvent(QCloseEvent arg) {
      super.closeEvent(arg);
      QMSProps.DONE = true;
   }

   public synchronized QRect getRect() {
      return rect();
   }

}
